package com.hg.oa.module.calendar.layer;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hg.oa.R;
import com.hg.oa.common.BaseFragment;
import com.hg.oa.module.calendar.adapter.CalendarListAdapter;
import com.hg.oa.util.ActivityHelper;
import com.hg.oa.util.DateUtil;
import com.melnykov.fab.FloatingActionButton;
import com.roomorama.caldroid.CaldroidFragment;
import com.roomorama.caldroid.CaldroidListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 我的日报界面
 * Created by fengxiang on 2016/9/1.
 */
public class DayReportFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    String tag = "DayReportFragment";
    Unbinder unbinder;

    @BindView(R.id.listView)
    ListView listView;

    @BindView(R.id.fab)
    FloatingActionButton fab;

    View headerView;
    View calendarContainer;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dayreport, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        headerView = inflater.inflate(R.layout.item_calendar_for_recyclerview, null);
        calendarContainer = headerView.findViewById(R.id.calendar1);
        fab.attachToListView(listView);
        listView.addHeaderView(headerView);
        listView.setAdapter(new CalendarListAdapter(getActivity()));
        listView.setOnItemClickListener(this);
        init();
        return rootView;

    }


    private boolean undo = false;
    private CaldroidFragment caldroidFragment;

    private void setCustomResourceForDates() {
        Calendar cal = Calendar.getInstance();
        List<String> dates = new ArrayList<>();
        dates.add(new String("2016-08-15"));
        dates.add(new String("2016-08-16"));
        dates.add(new String("2016-08-17"));
        dates.add(new String("2016-08-18"));
        dates.add(new String("2016-08-19"));

        dates.add(new String("2016-08-22"));
        dates.add(new String("2016-08-23"));
        dates.add(new String("2016-08-24"));
        dates.add(new String("2016-08-25"));
        dates.add(new String("2016-08-26"));

        dates.add(new String("2016-08-29"));
        dates.add(new String("2016-08-30"));
        dates.add(new String("2016-08-31"));
        dates.add(new String("2016-09-01"));


        if (caldroidFragment != null) {
            Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
            for (String dateStr : dates) {
                Date date = DateUtil.StringToDate(dateStr, DateUtil.DateStyle.YYYY_MM_DD);
                caldroidFragment.setBackgroundDrawableForDate(drawable, date);
                caldroidFragment.setTextColorForDate(R.color.white, date);
            }
        }
    }


    public void init() {
        caldroidFragment = new CaldroidSampleCustomFragment();
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
        caldroidFragment.setArguments(args);
        setCustomResourceForDates();
        FragmentTransaction t = getChildFragmentManager().beginTransaction();
        t.replace(calendarContainer.getId(), caldroidFragment);
        t.commit();
        // Setup Caldroid
        caldroidFragment.setCaldroidListener(new CaldroidListener() {

            @Override
            public void onSelectDate(Date date, View view) {
                Log.d(tag, "onSelectDate-->" + DateUtil.DateToString(date, DateUtil.DateStyle.YYYY_MM_DD));

            }

            @Override
            public void onChangeMonth(int month, int year) {
                Log.d(tag, "year-month-->" + year + " " + month);
            }

            @Override
            public void onLongClickDate(Date date, View view) {
                Log.d(tag, "onLongClickDate-->" + DateUtil.DateToString(date, DateUtil.DateStyle.YYYY_MM_DD));
                Intent intent = new Intent(getActivity(), DayInputActivity.class);
                ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
            }

            @Override
            public void onCaldroidViewCreated() {

            }

        });
    }

    @OnClick(R.id.fab)
    void onFabClick() {
        Intent intent = new Intent(getActivity(), DayInputActivity.class);
        ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 1) {
            DayInfoEditActivity.start(getActivity(), DayInputActivity.TYPE_LEVEL);
        }
        if (position == 2) {
            DayReportInfoActivity.start(getActivity());
        }
        if (position == 3) {
            DayInfoEditActivity.start(getActivity(), DayInputActivity.TYPE_REPORT);
        }
    }
}
